<#include "/java_copyright.include">
<#assign className = table.className>   
<#assign classNameLower = className?uncap_first> 
package ${basepackage}.biz.${namespace}.service.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import ${basepackage}.biz.${namespace}.dao.${className}Dao;
import ${basepackage}.biz.${namespace}.service.${className}Service;
import ${basepackage}.client.domain.${namespace}.${className};
import ${basepackage}.client.query.${namespace}.${className}Query;
import ${basepackage}.pojo.DataGrid;
import ${basepackage}.pojo.Pager;
import ${basepackage}.utils.LoginContextHolder;
import ${basepackage}.utils.StringConstant;
@Service("${classNameLower}Service")
@Transactional
public class ${className}ServiceImpl implements ${className}Service{
	@Autowired
	private ${className}Dao ${classNameLower}Dao;
	
	public void set${className}Dao(${className}Dao dao) {
		this.${classNameLower}Dao = dao;
	}

	public DataGrid datagrid(${className}Query ${classNameLower}Query) {
		DataGrid j = new DataGrid();
		Pager<${className}> pager  = find(${classNameLower}Query);
		j.setRows(getQuerysFromEntitys(pager.getRecords()));
		j.setTotal(pager.getTotalRecords());
		return j;
	}

	private List<${className}Query> getQuerysFromEntitys(List<${className}> ${classNameLower}s) {
		List<${className}Query> ${classNameLower}Querys = new ArrayList<${className}Query>();
		if (${classNameLower}s != null && ${classNameLower}s.size() > 0) {
			for (${className} tb : ${classNameLower}s) {
				${className}Query b = new ${className}Query();
				BeanUtils.copyProperties(tb, b);
				${classNameLower}Querys.add(b);
			}
		}
		return ${classNameLower}Querys;
	}

	private Pager<${className}> find(${className}Query ${classNameLower}Query) {
		Page<${className}> page = PageHelper.startPage(${classNameLower}Query.getPage().intValue(), ${classNameLower}Query.getRows().intValue());
		List<${className}> list = ${classNameLower}Dao.findList(${classNameLower}Query);
		Pager<${className}> pager = new Pager<${className}>();
		pager.setRecords(list);
		pager.setTotalRecords(page.getTotal());
		return  pager;
		
	}
	


	public ${className} add(${className}Query ${classNameLower}Query) {
		${className} t = new ${className}();
		BeanUtils.copyProperties(${classNameLower}Query, t);
		t.setActiveFlag(StringConstant.ONE);
		t.setCreateBy(LoginContextHolder.get().getUserId());
		t.setCreateByName(LoginContextHolder.get().getUserName());
		t.setCreateDate(new Date());
	    t.setModifiedBy(LoginContextHolder.get().getUserId());
	    t.setModifiedByName(LoginContextHolder.get().getUserName());
	    t.setModifiedDate(new Date());
		${classNameLower}Dao.save(t);
		${classNameLower}Query.set${table.pkColumn.columnName}(t.get${table.pkColumn.columnName}());
		return t;
		
	}

	public void update(${className}Query ${classNameLower}Query) {
		${className} t = ${classNameLower}Dao.getById(${classNameLower}Query.get${table.pkColumn.columnName}());
	    if(t != null) {
	    	BeanUtils.copyProperties(${classNameLower}Query, t);
		    t.setModifiedBy(LoginContextHolder.get().getUserId());
		    t.setModifiedByName(LoginContextHolder.get().getUserName());
		    t.setModifiedDate(new Date());
	    	${classNameLower}Dao.update(t);
		}
	}
	
	/**
	 * 根据条件更新部分字段   新建一个query封装需要更新的字段
	 * searchMap封装更新条件
	 * */
	public void updatePart(${className}Query ${classNameLower}Query) {
	    ${classNameLower}Dao.updatePart(${classNameLower}Query);
	}

	public void delete(${table.idColumn.javaType}[] ids) {
		${classNameLower}Dao.deleteAc(ids);
	}
	
	//逻辑删除 更新状态位
	public void deletePt(${table.idColumn.javaType}[] ids) {
		${classNameLower}Dao.deletePt(ids);
	}

	public ${className} get(${className}Query ${classNameLower}Query) {
		return ${classNameLower}Dao.getById(new Long(${classNameLower}Query.get${table.pkColumn.columnName}()));
	}

	public ${className} get(Long id) {
		return ${classNameLower}Dao.getById(id);
	}

	
	public List<${className}Query> listAll(${className}Query ${classNameLower}Query) {
	    List<${className}> list = ${classNameLower}Dao.findList(${classNameLower}Query);
		List<${className}Query> listQuery =getQuerysFromEntitys(list) ;
		return listQuery;
	}
	
	
}
